package com.ruoyi.order.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.OConvertUtils;
import com.ruoyi.order.domain.TbProductWholesale;
import com.ruoyi.order.mapper.TbProductWholeasalePriceMapper;
import com.ruoyi.order.mapper.TbProductWholesaleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.order.mapper.TbProductTypeMapper;
import com.ruoyi.order.domain.TbProductType;
import com.ruoyi.order.service.ITbProductTypeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品类型Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-19
 */
@Service
public class TbProductTypeServiceImpl implements ITbProductTypeService 
{
    @Autowired
    private TbProductTypeMapper tbProductTypeMapper;
    @Autowired
    private TbProductWholesaleMapper tbProductWholesaleMapper;
    @Autowired
    private TbProductWholeasalePriceMapper tbProductWholeasalePriceMapper;

    /**
     * 查询商品类型
     * 
     * @param id 商品类型主键
     * @return 商品类型
     */
    @Override
    public TbProductType selectTbProductTypeById(Long id)
    {
        TbProductWholesale tbProductWholesale = new TbProductWholesale();
        tbProductWholesale.setProductTypeId(id);
        List<TbProductWholesale> tbProductWholesales = tbProductWholesaleMapper.selectTbProductWholesaleList(tbProductWholesale);
        TbProductType tbProductType = tbProductTypeMapper.selectTbProductTypeById(id);
        if (OConvertUtils.isNotEmpty(tbProductType)){
            tbProductType.setWholesaleList(tbProductWholesales);
        }
        return tbProductType;
    }

    /**
     * 查询商品类型列表
     * 
     * @param tbProductType 商品类型
     * @return 商品类型
     */
    @Override
    public List<TbProductType> selectTbProductTypeList(TbProductType tbProductType)
    {
        List<TbProductType> tbProductTypes = tbProductTypeMapper.selectTbProductTypeList(tbProductType);
        List<Long> collect = tbProductTypes.stream().map(TbProductType::getId).collect(Collectors.toList());
        if (OConvertUtils.listIsNotEmpty(collect)){
            List<TbProductWholesale> tbProductWholesales = tbProductWholesaleMapper.selectTbproductWholesaleListByTypeIds(collect);
            for(TbProductType tbProductType1:tbProductTypes){
                List<TbProductWholesale> collect1 =new ArrayList<>();
                if (OConvertUtils.listIsNotEmpty(tbProductWholesales)){
                    collect1 = tbProductWholesales.stream().filter(tbProductWholesale -> tbProductWholesale.getProductTypeId().equals(tbProductType1.getId())).collect(Collectors.toList());
                }
                tbProductType1.setWholesaleList(collect1);
            }
        }
        return tbProductTypes;
    }

    /**
     * 新增商品类型
     * 
     * @param tbProductType 商品类型
     * @return 结果
     */
    @Override
    public int insertTbProductType(TbProductType tbProductType)
    {
        tbProductType.setCreateTime(DateUtils.getNowDate());
        int i = tbProductTypeMapper.insertTbProductType(tbProductType);
        List<TbProductWholesale> wholesaleList = tbProductType.getWholesaleList();
        wholesaleList.forEach(wholesale->{
            wholesale.setCreateTime(new Date());
            wholesale.setProductTypeId(tbProductType.getId());
            wholesale.setCreateBy(tbProductType.getCreateBy());
            wholesale.setCreateTime(DateUtils.getNowDate());
        });
        tbProductWholesaleMapper.insertBatch(wholesaleList);
        return i;
    }

    /**
     * 修改商品类型
     * 
     * @param tbProductType 商品类型
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateTbProductType(TbProductType tbProductType)
    {
        List<TbProductWholesale> wholesaleList = tbProductType.getWholesaleList();
        wholesaleList.forEach(wholesale->{
            wholesale.setProductTypeId(tbProductType.getId());
            wholesale.setCreateTime(new Date());
            wholesale.setCreateBy(tbProductType.getUpdateBy());
        });
        List<TbProductWholesale> addList = wholesaleList.stream().filter(tbProductWholesale -> OConvertUtils.isEmpty(tbProductWholesale.getId())).collect(Collectors.toList());
        List<TbProductWholesale> updateList = wholesaleList.stream().filter(tbProductWholesale -> OConvertUtils.isNotEmpty(tbProductWholesale.getId())).collect(Collectors.toList());
        if (OConvertUtils.listIsNotEmpty(addList)){
            tbProductWholesaleMapper.insertBatch(addList);
        }
        for (TbProductWholesale tbProductWholesale:updateList){
            tbProductWholesaleMapper.updateTbProductWholesale(tbProductWholesale);
        }
        return tbProductTypeMapper.updateTbProductType(tbProductType);
    }

    /**
     * 批量删除商品类型
     * 
     * @param ids 需要删除的商品类型主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteTbProductTypeByIds(Long[] ids)
    {
        List<Long> list = Arrays.asList(ids);
        List<TbProductWholesale> tbProductWholesales = tbProductWholesaleMapper.selectTbproductWholesaleListByTypeIds(list);
        // 删除批发价
        if (OConvertUtils.listIsNotEmpty(tbProductWholesales)){
            List<Long> collect = tbProductWholesales.stream().map(TbProductWholesale::getId).collect(Collectors.toList());
            tbProductWholeasalePriceMapper.deleteTbProductWholeasalePriceByWholeasaleIds(collect);
        }
        // 删除规格
        tbProductWholesaleMapper.deleteTbProductWholesaleByTypeIds(ids);
        return tbProductTypeMapper.deleteTbProductTypeByIds(ids);
    }

    /**
     * 删除商品类型信息
     * 
     * @param id 商品类型主键
     * @return 结果
     */
    @Override
    public int deleteTbProductTypeById(Long id)
    {
        return tbProductTypeMapper.deleteTbProductTypeById(id);
    }
}
